import { State } from "framework/FSM/AState";
import { ActionArgHelper } from "../../../ActionManager/ActionArgHelper";
import { EntityHelper } from "utils/entity/EntityHelper";
import { ActionUtils } from "../utils/ActionUtil";
import { PositionHelper } from "utils/position/PositionHelper";
 

/**
 * 行为模式
 * 采集行为
 * 升级行为
 */ 
export class BuildTask<T extends IStateArg<Creep>>  extends State<T>
{

    //
    public  getActions(arg:T):Action[]
    {
        const memory = arg.entity.getNodeMemory();
        if(!memory.taskid)
        {
            return [];
        }
        const task =arg.task as ITask<ICreepTaskData<"build">>;
        //const task = arg.entity.ownerRoom().findTaskById<ITask<ICreepTaskData<"build">>>(memory.taskid,"creep");
        if(!task)
        {
            // 查找不到任务
            return []
        }
        const actions = ActionUtils.getEscapeSourceKeeperAction(arg.entity);
        if(actions)
        {
            return actions
        }
       
        // 建造任务
        // 1.能量不足。则取取能量
        // 1.a sourceid 存在。且能量足够 => withdraw
        // 1.b !(sourceid 存在。且能量足够) => 更新sourceId
        const creep = arg.entity;
       
        if(creep.store.getUsedCapacity(RESOURCE_ENERGY)==0)
        {
            creep.memory.state = 0;
            creep.memory.sourceId = undefined;
        }
        else  if(creep.store.getFreeCapacity(RESOURCE_ENERGY)==0)
        {
            creep.memory.state = 1;
            
        }
        if(creep.memory.state)
        {
            
            if(!creep.memory.targetId)
            {
                // const neartarget = _.min(task.data.arg.Objects,e=>creep.pos.distance(e) );
                creep.memory.targetId =task.data.arg.Objects[0].id;
            }
            const siteobject = Game.getObjectById(creep.memory.targetId as Id<ConstructionSite>) ;
            if(!siteobject)
            {
                creep.memory.targetId=undefined;
                return [ActionArgHelper.actionMoveRoom(creep, task.data.room)];
                
            }
            // 一定有视野
            if( siteobject.room?.name == creep.room.name)
            {
                return [ActionArgHelper.actionBuild(creep,siteobject)]
            }
            else 
            {
                 
                return [ActionArgHelper.actionMoveRoom(creep, siteobject!.pos!.roomName)];
            }
            
        }
        else
        {
            /**
             * 是否在目标房间
             */

            const workRoom = task.data.arg.Objects[0].roomName;
            const room =Game.rooms[workRoom];
           
            if(room && creep.room.name == workRoom)
            {
                if(room.getType() ==1)
                {
                    // 取资源
                    if(!creep.memory.sourceId)
                    {   
                        const sourceid = EntityHelper.getWithdrawObject2(creep.ownerRoom(),creep,creep.store.getCapacity()*1.5);
                        if(sourceid)
                        {
                            creep.memory.sourceId = sourceid;
                        }
                    }
                    const store =  Game.getObjectById(creep.memory.sourceId as Id<AnyStoreStructure>) ;
                    if(store)
                    {
                        if(store.store.getUsedCapacity(RESOURCE_ENERGY)> creep.store.getCapacity()*1.5)
                        {
                        
                            return [ActionArgHelper.warp(ActionArgHelper.actionWithdraw(creep,store,[{resourceType:RESOURCE_ENERGY}]),undefined,(arg:IActionCMD<'withdraw'>)=>
                        {
                            
                            if(arg.arg.entity.store.getUsedCapacity(RESOURCE_ENERGY)>0)
                            {
                                creep.memory.state=1;
                                return true;
                            }
                            return false
                        })]
                        }
                        else
                        {
                            creep.memory.sourceId = undefined;
                        }
                    }
                    const spawn = arg.entity.getEntity("SpawnEntity");
                    const res = spawn.getPickUpResource();
                    if(res)
                    {
                        return [ActionArgHelper.actionPickup(creep, res)]
                    }
                }
                else 
                {
                    // 不是自己的房间则。查找source
                    const actions = ActionUtils.getNormalAction(creep,room);
                    return actions
                }
            }
            else
            {
                // creep.memory.targetId=undefined;
                // site 所在的房间没有视野
                return [ActionArgHelper.actionMoveRoom(creep, workRoom)];
            }
            
        }
        // 能量足够。则进行工作
        /**
         * targetId 不存在 -> targetId
         * targetId  存在 -> 进行工作
        */
        return [ ];
    }
}
